home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / toolkit.jar / content / global / commonDialog.js < prev    next >
Encoding:
JavaScript  |  2001-04-12  |  8.5 KB  |  260 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape Communications
  16.  * Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Alec Flett  <alecf@netscape.com>
  22.  *  Ben Goodger <ben@netscape.com>
  23.  *  Blake Ross  <blakeross@telocity.com>
  24.  */
  25.  
  26. var gCommonDialogParam;
  27.  
  28. function commonDialogOnLoad()
  29. {
  30.   doSetOKCancel(commonDialogOnOK, commonDialogOnCancel, commonDialogOnButton2, commonDialogOnButton3);
  31.   gCommonDialogParam = window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  32.  
  33.   // display the main text
  34.   var messageText = gCommonDialogParam.GetString(0);
  35.   var messageParent = document.getElementById("info.box");
  36.   var messageParagraphs = messageText.split("\n");
  37.   var i;
  38.  
  39.   for (i = 0; i < messageParagraphs.length; i++) {
  40.     var htmlNode = document.createElement("html");
  41.     //htmlNode.setAttribute("style", "max-width: 45em;");
  42.     var text = document.createTextNode(messageParagraphs[i]);
  43.     htmlNode.appendChild(text);
  44.     messageParent.appendChild(htmlNode);
  45.   }
  46.  
  47.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  48.  
  49.   // set the window title
  50.   window.title = gCommonDialogParam.GetString(12);
  51.  
  52.   // set the icon
  53.   var iconElement = document.getElementById("info.icon");
  54.   var iconClass = gCommonDialogParam.GetString(2);
  55.   if (!iconClass)
  56.     iconClass = "message-icon";
  57.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  58.  
  59.   // set the number of command buttons
  60.   var nButtons = gCommonDialogParam.GetInt(2);
  61.   if (nButtons == 1) hideElementById("cancel");
  62.   switch (nButtons) {
  63.     case 4:
  64.       unHideElementByID("Button3");
  65.       document.getElementById("Button3").label = gCommonDialogParam.GetString(11);
  66.       // fall through
  67.     case 3:
  68.       unHideElementByID("Button2");
  69.       document.getElementById("Button2").label = gCommonDialogParam.GetString(10);
  70.       // fall through
  71.     default:
  72.     case 2:
  73.       var string = gCommonDialogParam.GetString(8);
  74.       if (string)
  75.         document.getElementById("ok").label = string;
  76.       // fall through
  77.     case 1:
  78.       string = gCommonDialogParam.GetString(9);
  79.       if (string)
  80.         document.getElementById("cancel").label = string;
  81.       break;
  82.   }
  83.  
  84.   // initialize the checkbox
  85.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  86.  
  87.   // initialize the edit fields
  88.   var nEditFields = gCommonDialogParam.GetInt(3);
  89.  
  90.   switch (nEditFields) {
  91.     case 2:
  92.       var containerID, fieldID, labelID;
  93.  
  94.       if (gCommonDialogParam.GetInt(4) == 1) {
  95.         // two password fields ('password' and 'retype password')
  96.         var password2Container = document.getElementById("password2EditField");
  97.         password2Container.removeAttribute("collapsed");
  98.         var password2Field = document.getElementById("dialog.password2");
  99.         password2Field.value = gCommonDialogParam.GetString(7);
  100.  
  101.         var password2Label = gCommonDialogParam.GetString(5);
  102.         if (password2Label)
  103.           setElementText("password2.text", password2Label);
  104.  
  105.         containerID = "password1EditField";
  106.         fieldID = "dialog.password1";
  107.         labelID = "password1.text";
  108.       }
  109.       else {
  110.         // one login field and one password field
  111.         var passwordContainer = document.getElementById("password1EditField");
  112.         passwordContainer.removeAttribute("collapsed");
  113.         var passwordField = document.getElementById("dialog.password1");
  114.         passwordField.value = gCommonDialogParam.GetString(7);
  115.  
  116.         var passwordLabel = gCommonDialogParam.GetString(5);
  117.         if (passwordLabel)
  118.           setElementText("password1.text", passwordLabel);
  119.  
  120.         containerID = "loginEditField";
  121.         fieldID = "dialog.loginname";
  122.         labelID = "login.text";
  123.       }
  124.  
  125.       unHideElementByID(containerID);
  126.       var field = document.getElementById(fieldID);
  127.       field.value = gCommonDialogParam.GetString(6);
  128.  
  129.       var label = gCommonDialogParam.GetString(4);
  130.       if (label)
  131.         setElementText(labelID, label);
  132.       field.focus();
  133.       break;
  134.     case 1:
  135.       var editFieldIsPassword = gCommonDialogParam.GetInt(4);
  136.       if (editFieldIsPassword == 1) {
  137.         containerID = "password1EditField";
  138.         fieldID = "dialog.password1";
  139.         setElementText("password1.text", ""); // hide the meaningless text
  140.       }
  141.       else {
  142.         containerID = "loginEditField";
  143.         fieldID = "dialog.loginname";
  144.         setElementText("login.text", gCommonDialogParam.GetString(4));
  145.       }
  146.  
  147.       unHideElementByID(containerID);
  148.       field = document.getElementById(fieldID);
  149.       field.value = gCommonDialogParam.GetString(6);
  150.       field.focus();
  151.       break;
  152.   }
  153.  
  154.   // set the pressed button to cancel to handle the case where the close box is pressed
  155.   gCommonDialogParam.SetInt(0, 1);
  156.  
  157.   // set default focus
  158.   // preferred order is textbox1, textbox2, textbox 3, OK, cancel, button2, button3
  159.   var visibilityList = ["loginEditField", "password1EditField", "password2EditField", "ok",
  160.                          "cancel", "Button2", "Button3"]
  161.   var focusList      = ["dialog.loginname", "dialog.password1", "dialog.password2", "ok",
  162.                          "cancel", "Button2", "Button3"]
  163.  
  164.   for (i = 0; i < visibilityList.length; i++) {
  165.     if (isVisible(visibilityList[i])) {
  166.       document.getElementById(focusList[i]).focus();
  167.       break;
  168.     }
  169.   }
  170. }
  171.  
  172. function setCheckbox(aChkMsg, aChkValue)
  173. {
  174.   if (aChkMsg) {
  175.     var checkboxElement = document.getElementById("checkbox");
  176.     unHideElementByID("checkboxContainer");
  177.     checkboxElement.label = aChkMsg;
  178.     checkboxElement.checked = aChkValue > 0 ? true : false;
  179.   }
  180. }
  181.  
  182. function unHideElementByID(aElementID)
  183. {
  184.   var element = document.getElementById(aElementID);
  185.   element.removeAttribute("collapsed");
  186. }
  187.  
  188. function hideElementById(aElementID)
  189. {
  190.   var element = document.getElementById(aElementID)
  191.   element.setAttribute("collapsed", "true");
  192. }
  193.  
  194. function isVisible(aElementId)
  195. {
  196.   var element = document.getElementById(aElementId);
  197.   if (element) {
  198.     // XXX check for display:none when getComputedStyle() works properly
  199.     return !(element.getAttribute("collapsed") || element.getAttribute("hidden"));
  200.   }
  201.   return false;   // doesn't exist, so not visible
  202. }
  203.  
  204. function onCheckboxClick(aCheckboxElement)
  205. {
  206.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  207. }
  208.  
  209. function setElementText(aElementID, aValue, aChildNodeFlag)
  210. {
  211.   var element = document.getElementById(aElementID);
  212.   if (!aChildNodeFlag && element)
  213.     element.setAttribute("value", aValue);
  214.   else if (aChildNodeFlag && element)
  215.     element.appendChild(document.createTextNode(aValue));
  216. }
  217.  
  218. function commonDialogOnOK()
  219. {
  220.   gCommonDialogParam.SetInt(0, 0);
  221.   var numEditfields = gCommonDialogParam.GetInt(3);
  222.   var editfield1Password = gCommonDialogParam.GetInt(4);
  223.   if (numEditfields == 2) {
  224.     var editField2;
  225.     if (editfield1Password == 1)
  226.       {
  227.         // we had two password fields
  228.         editField2 = document.getElementById("dialog.password2");
  229.       }
  230.     else
  231.       {
  232.         // we only had one password field (and one login field)
  233.         editField2 = document.getElementById("dialog.password1");
  234.       }
  235.     gCommonDialogParam.SetString(7, editField2.value);
  236.   }
  237.   var editField1 = editfield1Password == 1 ? document.getElementById("dialog.password1") :
  238.                                              document.getElementById("dialog.loginname");
  239.   gCommonDialogParam.SetString(6, editField1.value);
  240.   return true;
  241. }
  242.  
  243. function commonDialogOnCancel()
  244. {
  245.   gCommonDialogParam.SetInt(0, 1);
  246.   return true;
  247. }
  248.  
  249. function commonDialogOnButton2()
  250. {
  251.   gCommonDialogParam.SetInt(0, 2);
  252.   return true;
  253. }
  254.  
  255. function commonDialogOnButton3()
  256. {
  257.   gCommonDialogParam.SetInt(0, 3);
  258.   return true;
  259. }
  260.